/**
 * 
 */
package sap.provider;

import com.sap.mw.jco.JCO;

import sap.Connection;
import data.ProjectProfile;

/**
 * @author sebastian
 *
 */
public final class Profiles {
	// Cache
	private static ProjectProfile[] profiles = null;
	
	public static ProjectProfile[] get() {
		if (profiles != null)
			return profiles;
		
		Connection con = Connection.getInstance();

		JCO.Function function = con.getFunction("RFC_READ_TABLE");

		// fill import parameter
		function.getImportParameterList().setValue("TCJ4T", "QUERY_TABLE");
		JCO.ParameterList tables = function.getTableParameterList();

		JCO.Table t = tables.getTable("OPTIONS");
		t.appendRow();
		t.setValue("SPRAS = '" + con.getLanguage()
				+ "'", "TEXT");

		t = tables.getTable("FIELDS");
		t.appendRow();
		t.setValue("PROFIDPROJ", "FIELDNAME");
		t.appendRow();
		t.setValue("PROFI_TXT", "FIELDNAME");

		con.execute(function);

		t = function.getTableParameterList().getTable("DATA");
		
		int numRows = t.getNumRows();
		profiles = new ProjectProfile[numRows];
		for(int i=0; i<numRows; i++) {
			String line = t.getString(0);
			profiles[i] = new ProjectProfile(line.substring(0, 7).trim(), line.substring(7));
			t.nextRow();
		}
		
		return profiles;
	}
}
